import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'dart:convert';

import 'package:flutter_app/movie/waterfall.dart';

class detials extends StatefulWidget {
  var id;
  detials(this.id);
  detialsWidget createState() => new detialsWidget(id);
}

class detialsWidget extends State<detials> {
  Widget _body;
  var id ;

  detialsWidget(this.id);
  getDate() async {
    Dio dio = new Dio();
    List<Response> response = await Future.wait([
      dio.get(
          "https://api.themoviedb.org/3/movie/$id?api_key=80ac1d0d481f2a1e3e51b5cf5703cf8b&language=zh-CN"),
      dio.get(
          "https://api.themoviedb.org/3/movie/$id/credits?api_key=80ac1d0d481f2a1e3e51b5cf5703cf8b")
    ]);
    var _data = JsonDecoder().convert(response.toString());
    List genres = _data[0]["genres"];
    var overview = _data[0]["overview"];
    var vote_average = _data[0]["vote_average"];
    var backdrop_path = _data[0]["backdrop_path"];
    var poster_path = _data[0]["poster_path"];
    var release_date = _data[0]["release_date"];
    var title = _data[0]["title"];
    List cast = _data[1]['cast'];
    List crew = _data[1]["crew"];
    print(cast[0]["profile_path"]);
    setState(() {
      _body = Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Positioned(
              child: Container(
            decoration: BoxDecoration(
                color: Colors.white,
                gradient: LinearGradient(
                    begin: FractionalOffset.bottomCenter,
                    end: FractionalOffset.topCenter,
                    colors: [
                      Colors.purple,
                      Colors.purple[500],
                      Colors.purple[400],
                      Colors.purple[300]
                    ],
                    stops: [
                      0.0,
                      0.25,
                      0.5,
                      0.75
                    ])),
          )),
          Positioned(
            child: Container(
              child: FadeInImage(
                height: double.infinity,
                width: double.infinity,
                image: NetworkImage(
                    "https://image.tmdb.org/t/p/w500" + backdrop_path,),
                placeholder: AssetImage('assets/loading.gif'),
                fit: BoxFit.cover,
              ),
            ),
          ),
          Positioned(
            bottom: 15.0,
            child: SizedBox(
              height: MediaQuery.of(context).size.height * 7/ 10,
              width: MediaQuery.of(context).size.width * 9 / 10,
              child: Card(
                elevation: 15.0,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(14.0))),
                child: Column(
                  children: <Widget>[
                    SizedBox(
                      width: double.infinity,
                      child: Container(
                        padding: EdgeInsets.fromLTRB(title.length<=3?150:140, 8.0, 0, 0),
                        child: Text(
                          title,
                          style: TextStyle(fontSize:title.length>=8?20:25),
                            maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ),
                    Container(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Container(
                            padding: EdgeInsets.only(left: 50),
                            child: Text('评分: ' + "$vote_average"),
                          ),
                          Container(
                            padding: EdgeInsets.fromLTRB(0, 8.0, 0, 8.0),
                            child: Icon(
                              Icons.star,
                              color: Colors.green,
                            ),
                          )
                        ],
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 0),
                      child: SizedBox(
                        height: 35,
                        width: double.infinity,
                        child: ListView.builder(
                          scrollDirection: Axis.horizontal,
                          itemBuilder: (BuildContext context, int position) {
                            return Container(
                              padding: EdgeInsets.fromLTRB(5.0, 0, 5.0, 0),
                              child: SizedBox(
                                height: 35,
                                width: 60,
                            child: RaisedButton(
                                  shape: RoundedRectangleBorder(
                                      borderRadius:
                                          BorderRadius.circular(20.0)),
                                  child: Text(
                                    genres[position]["name"],
                                    style: TextStyle(color: Colors.white),
                                  ),
                                  color: Colors.purple,
                                  onPressed: () {},
                                ),
                              ),
                            );
                          },
                          itemCount: genres.length,
                        ),
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.fromLTRB(5.0, 10.0, 0, 0),
                      child: SizedBox(
                        width: double.infinity,
                        child: Text(
                          'Overview',
                          style: TextStyle(fontSize: 20.0),
                        ),
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
                      child: SizedBox(
                        height: 100,
                        width: double.infinity,
                        child: SingleChildScrollView(
                          child: Container(
                            child: Text(overview),
                          ),
                        ),
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
                      child: SizedBox(
                        width: double.infinity,
                        child: Text(
                          'Release data: ' + release_date,
                          style: TextStyle(fontSize: 18.0),
                        ),
                      ),
                    ),
                    Container(
                      child: SizedBox(
                        height: 30.0,
                        width: double.infinity,
                        child: Stack(
                          children: <Widget>[
                            Positioned(
                              left: 10.0,
                              child: Text(
                                'Cast',
                                style: TextStyle(fontSize: 18.0),
                              ),
                            ),
                            Positioned(
                              right: 10.0,
                              child: Container(
                                child: GestureDetector(
                                  onTap: (){
                                    Navigator.push(context, new MaterialPageRoute(builder: (context)=>waterfall(id)));
                                  },
                                  child: Text('See full cast & crew'),
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                    Container(
                      child: SizedBox(
                        width: double.infinity,
                        height: 100,
                        child: ListView.builder(
                            scrollDirection: Axis.horizontal,
                            itemCount: cast.length,
                            itemBuilder: (BuildContext context, int position) {
                              return new Container(
                                padding: EdgeInsets.fromLTRB(5.0, 0, 5.0, 0),
                                child: Column(
                                  children: <Widget>[
                                    Container(
                                      child: SizedBox(
                                        height: 70,
                                        width: 50,
                                        child: ClipRRect(
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(5.0)),
                                          child:cast[position]["profile_path"]==null?Image(image: AssetImage('assets/na.jpg')):FadeInImage(
                                            height: double.infinity,
                                            width: double.infinity,
                                            image: NetworkImage(
                                                "https://image.tmdb.org/t/p/w500" +
                                                    cast[position]
                                                        ["profile_path"]),
                                            placeholder: AssetImage(
                                                'assets/loading.gif'),
                                            fit: BoxFit.cover,
                                          ),
                                        ),
                                      ),
                                    ),
                                    Container(
                                      child: SizedBox(
                                        height: 30,
                                        width: 50,
                                        child: Center(
                                          child: Text(
                                            cast[position]["name"],
                                            overflow: TextOverflow.ellipsis,
                                            maxLines: 1,
                                          ),
                                        ),
                                      ),
                                    )
                                  ],
                                ),
                              );
                            }),
                      ),
                    )
                  ],
                ),
              ),
            ),
          ),
          Positioned(
            left: 55.0,
            top: 105.0,
            child: Container(
              child: SizedBox(
                width: 100,
                height: 160,
                child: ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(14.0)),
                  child: FadeInImage(
                    placeholder: AssetImage('assets/loading.gif'),
                    image: NetworkImage("https://image.tmdb.org/t/p/w500"+poster_path),
                    fit: BoxFit.cover,
                    height: double.infinity,
                    width: double.infinity,
                  ),
                ),
              ),
            ),
          ),
          Positioned(
            top: 20,
            left: 10,
            child: Container(
              child:  IconButton(
                icon: Icon(Icons.arrow_back,color: Colors.white,),
                onPressed: (){
                  Navigator.pop(context);
                },
              )
            ),
          )
        ],
      );
    });
  }

  void initState() {
    _body = Center(
      child: CircularProgressIndicator(),
    );
    getDate();
  }

  Widget build(BuildContext context) {
    return new Scaffold(
      body: _body,
    );
  }
}
